﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Player
{
    #region Constants

    private const float _delayTime = 0.03f;

    #endregion 

    #region Atributos

    private Sprite2D _lifeBar;
    private Sprite2D _backLifeBar;

    public List<Shoot> Shoots { private set; get; }

    private Vector2 _position;
    private Sprite2D _playerSprite;
    
    private float _angle;
    private float _shootDelay;
	
	public const int MAX_LIFE = 100;
	private int _playerLife;

    #endregion 
	
	#region Propriedades
	
	public int PlayerLife 
	{ 
		get { return _playerLife; }
		set
		{
			_playerLife = value;
			
			if(_playerLife < 0)
				_playerLife = 0;
		}
	}
	
	#endregion

    #region Construtor
    
    public Player()
    {
        //Imagem do canhao
        _playerSprite=  new Sprite2D("Images/Game Elements/aviao", new Vector2(0, (Constantes.TELA_ALTURA - 172) / 2));
    
        //Barra de vida
        _backLifeBar = new Sprite2D("Images/GUI/PlayerBackLifeBar", new Vector2(10, 10));
        _lifeBar = new Sprite2D("Images/GUI/PlayerLifeBar", new Vector2(10,10));

        //Lista de tiros!
        Shoots = new List<Shoot>();

        PlayerLife = MAX_LIFE;
    }
    
    #endregion 

    #region Update
    
    public void Update()
    {
        //Pega o angulo para rotação do player!
        GetRotationAngle();
        
        //Controla o input do usuario e os tiros disparados
        ShootController();
    }

    private void GetRotationAngle()
    {
        Vector2 center = new Vector2(_playerSprite.Position.x + _playerSprite.Largura / 2, _playerSprite.Position.y + _playerSprite.Altura / 2);
        Vector2 mousePosition = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);

        var lookDirection = mousePosition - center;
        lookDirection = lookDirection.normalized;

        //Calcula o angulo que o player deve olhar
        _angle = (float)Math.Atan2(lookDirection.y, lookDirection.x);

        //Convertendo para grau
        _angle = (float)(_angle * 180 / Math.PI);

        //Limitando a rotacao
        if (_angle < -45) _angle = -45;
        else if (_angle > 45) _angle = 45;

        //aplica o valor da rotação atual ao player
        _playerSprite.Rotacao = _angle;
    }

    private bool _press;

    private void ShootController()
    {
		if(PlayerLife > 0)
		{
	        //Verifica se foi precionado
	        if (Input.GetMouseButtonDown(0))
	            _press = true;
	  
	        //Verifica se continua precionado
	        if (_press)
	            _shootDelay += Time.deltaTime;
	  
	        //Verifica se deu release
	        if (Input.GetMouseButtonUp(0))
	        {
	            _press = false;
	            _shootDelay = 0;
	        }
	
	        //Disparo de tiros
	        if (_shootDelay > _delayTime)
	        {
	           _shootDelay = 0;
	            Shoot s = new Shoot(_angle, new Vector2(1, Screen.height - Input.mousePosition.y), new Vector2(_playerSprite.Largura, (Screen.height / 2) + _angle * 5));
	            Shoots.Add(s);
	        }
	        
	        for (int i = 0; i < Shoots.Count; i++)
	        {
	            Shoots[i].Update();
	
	            if (Shoots[i].ImDead)
	            {
	                Shoots.RemoveAt(i);
	            }
	        }
		}
    }

    #endregion 

    #region GUI
    
    public void Draw()
    {
        DrawPlayer();

        DrawShoots();

        LifeBar();
    }

    private void DrawPlayer()
    {
        _playerSprite.Draw(new Vector2(0, (Constantes.TELA_ALTURA / 2)));
    }

    private void DrawShoots()
    {
        for (int i = 0; i < Shoots.Count; i++)
        {
            Shoots[i].Draw();
        }
    }

    private void LifeBar()
    {
        _backLifeBar.Draw();
		
		float lifePercent = (float)PlayerLife / (float)MAX_LIFE;
		
        _lifeBar.Draw(new Rect(0, 0, lifePercent, 1));
    }

    #endregion 

    #region Métodos



    #endregion
}
